using Microsoft.Phone.Controls.Primitives;
using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
namespace Microsoft.Phone.Controls
{
	[TemplatePart(Name = "Switch", Type = typeof(ToggleSwitchButton)), TemplateVisualState(Name = "Disabled", GroupName = "CommonStates"), TemplateVisualState(Name = "Normal", GroupName = "CommonStates")]
	public class ToggleSwitch : ContentControl
	{
		private const string CommonStates = "CommonStates";
		private const string NormalState = "Normal";
		private const string DisabledState = "Disabled";
		private const string SwitchPart = "Switch";
		public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register("Header", typeof(object), typeof(ToggleSwitch), new PropertyMetadata(null));
		public static readonly DependencyProperty HeaderTemplateProperty = DependencyProperty.Register("HeaderTemplate", typeof(DataTemplate), typeof(ToggleSwitch), new PropertyMetadata(null));
		public static readonly DependencyProperty SwitchForegroundProperty = DependencyProperty.Register("SwitchForeground", typeof(Brush), typeof(ToggleSwitch), null);
		public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register("IsChecked", typeof(bool?), typeof(ToggleSwitch), new PropertyMetadata(false, new PropertyChangedCallback(ToggleSwitch.OnIsCheckedChanged)));
		private ToggleSwitchButton _toggleButton;
		private bool _wasContentSet;
		public event EventHandler<RoutedEventArgs> Checked;
		public event EventHandler<RoutedEventArgs> Unchecked;
		public event EventHandler<RoutedEventArgs> Indeterminate;
		public event EventHandler<RoutedEventArgs> Click;
		public object Header
		{
			get
			{
				return base.GetValue(ToggleSwitch.HeaderProperty);
			}
			set
			{
				base.SetValue(ToggleSwitch.HeaderProperty, value);
			}
		}
		public DataTemplate HeaderTemplate
		{
			get
			{
				return (DataTemplate)base.GetValue(ToggleSwitch.HeaderTemplateProperty);
			}
			set
			{
				base.SetValue(ToggleSwitch.HeaderTemplateProperty, value);
			}
		}
		public Brush SwitchForeground
		{
			get
			{
				return (Brush)base.GetValue(ToggleSwitch.SwitchForegroundProperty);
			}
			set
			{
				base.SetValue(ToggleSwitch.SwitchForegroundProperty, value);
			}
		}
		[TypeConverter(typeof(NullableBoolConverter))]
		public bool? IsChecked
		{
			get
			{
				return (bool?)base.GetValue(ToggleSwitch.IsCheckedProperty);
			}
			set
			{
				base.SetValue(ToggleSwitch.IsCheckedProperty, value);
			}
		}
		private static void OnIsCheckedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			ToggleSwitch toggleSwitch = (ToggleSwitch)d;
			if (toggleSwitch._toggleButton != null)
			{
				toggleSwitch._toggleButton.set_IsChecked((bool?)e.get_NewValue());
			}
		}
		public ToggleSwitch()
		{
			base.set_DefaultStyleKey(typeof(ToggleSwitch));
		}
		private void SetDefaultContent()
		{
			Binding binding = new Binding("IsChecked");
			binding.set_Source(this);
			binding.set_Converter(new OffOnConverter());
			Binding binding2 = binding;
			base.SetBinding(ContentControl.ContentProperty, binding2);
		}
		private void ChangeVisualState(bool useTransitions)
		{
			if (base.get_IsEnabled())
			{
				VisualStateManager.GoToState(this, "Normal", useTransitions);
				return;
			}
			VisualStateManager.GoToState(this, "Disabled", useTransitions);
		}
		protected override void OnContentChanged(object oldContent, object newContent)
		{
			base.OnContentChanged(oldContent, newContent);
			this._wasContentSet = true;
			if (DesignerProperties.get_IsInDesignTool() && newContent == null && base.GetBindingExpression(ContentControl.ContentProperty) == null)
			{
				this.SetDefaultContent();
			}
		}
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			if (!this._wasContentSet && base.GetBindingExpression(ContentControl.ContentProperty) == null)
			{
				this.SetDefaultContent();
			}
			if (this._toggleButton != null)
			{
				this._toggleButton.remove_Checked(new RoutedEventHandler(this.OnChecked));
				this._toggleButton.remove_Unchecked(new RoutedEventHandler(this.OnUnchecked));
				this._toggleButton.remove_Indeterminate(new RoutedEventHandler(this.OnIndeterminate));
				this._toggleButton.remove_Click(new RoutedEventHandler(this.OnClick));
			}
			this._toggleButton = (base.GetTemplateChild("Switch") as ToggleSwitchButton);
			if (this._toggleButton != null)
			{
				this._toggleButton.add_Checked(new RoutedEventHandler(this.OnChecked));
				this._toggleButton.add_Unchecked(new RoutedEventHandler(this.OnUnchecked));
				this._toggleButton.add_Indeterminate(new RoutedEventHandler(this.OnIndeterminate));
				this._toggleButton.add_Click(new RoutedEventHandler(this.OnClick));
				this._toggleButton.set_IsChecked(this.IsChecked);
			}
			base.add_IsEnabledChanged(delegate(object sender, DependencyPropertyChangedEventArgs e)
			{
				this.ChangeVisualState(true);
			});
			this.ChangeVisualState(false);
		}
		private void OnChecked(object sender, RoutedEventArgs e)
		{
			this.IsChecked = new bool?(true);
			SafeRaise.Raise<RoutedEventArgs>(this.Checked, this, e);
		}
		private void OnUnchecked(object sender, RoutedEventArgs e)
		{
			this.IsChecked = new bool?(false);
			SafeRaise.Raise<RoutedEventArgs>(this.Unchecked, this, e);
		}
		private void OnIndeterminate(object sender, RoutedEventArgs e)
		{
			this.IsChecked = default(bool?);
			SafeRaise.Raise<RoutedEventArgs>(this.Indeterminate, this, e);
		}
		private void OnClick(object sender, RoutedEventArgs e)
		{
			SafeRaise.Raise<RoutedEventArgs>(this.Click, this, e);
		}
		public override string ToString()
		{
			return string.Format(CultureInfo.get_InvariantCulture(), "{{ToggleSwitch IsChecked={0}, Content={1}}}", new object[]
			{
				this.IsChecked,
				base.get_Content()
			});
		}
	}
}
